package com.jcx.ldzj.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jcx.ldzj.emun.MyCode;
import com.jcx.ldzj.emun.SystemEnum;
import com.jcx.ldzj.entity.DepartmentInfo;
import com.jcx.ldzj.entity.DeptUser;
import com.jcx.ldzj.service.IDepartmentInfoService;
import com.jcx.ldzj.service.IDeptUserService;
import com.jcx.ldzj.service.IUserInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Ð¡Ìì
 * @since 2021-01-20
 */
@RestController
@Slf4j
@AllArgsConstructor
@RequestMapping("/department-info")
@Api(value = "部门信息", tags = "部门信息")
public class DepartmentInfoController extends ApiController {
        @Autowired
        private IDepartmentInfoService iDepartmentInfoService;
    @Autowired
    private IDeptUserService iDeptUserService;
    @Autowired
    private IUserInfoService iUserInfoService;
       /**
       * 列表
       * @param pageNum
       * @param pageSize
       * @param departmentInfo
       * @return
       */
       @GetMapping("/page")
       @RequiresPermissions("basic:department:page")
       @ApiOperation(value = "列表", notes = "传入DepartmentInfo对象")
       public R<IPage<DepartmentInfo>> page(Integer pageNum, Integer pageSize,DepartmentInfo departmentInfo) {
            QueryWrapper<DepartmentInfo> queryWrapper=new QueryWrapper<>(departmentInfo);
            queryWrapper.orderByDesc("update_time_");

           if(departmentInfo.getDepName()!=null){
               queryWrapper.like("dep_name_",departmentInfo.getDepName());
               departmentInfo.setDepName(null);
           }
            IPage<DepartmentInfo> page = iDepartmentInfoService.page(new Page<>(pageNum, pageSize), queryWrapper);
            return R.ok(page);
       }
       /**
       *
       * @param id
       * @return
       */
       @GetMapping("/{id}")
       @RequiresPermissions("basic:department:view")
       @ApiOperation(value = "详情", notes = "传入id")
       public R<DepartmentInfo> view(@PathVariable("id") String id) {
          DepartmentInfo user = iDepartmentInfoService.getById(id);
          return R.ok(user);
        }

        /**
        * 新增
        * @param departmentInfo
        * @return
        */
        @PostMapping
        @RequiresPermissions("basic:department:add")
        @ApiOperation(value = "新增", notes = "传入DepartmentInfo")
        public R add(@Valid @RequestBody DepartmentInfo departmentInfo) {
          return this.success(iDepartmentInfoService.save(departmentInfo));
        }


        /**
        * 修改
        * @param departmentInfo
        * @return
        */
        @PutMapping
        @ApiOperation(value = "修改", notes = "传入DepartmentInfo")
        @RequiresPermissions("basic:department:update")
        public R edit(@Valid @RequestBody DepartmentInfo departmentInfo) {
            DepartmentInfo byId = iDepartmentInfoService.getById(departmentInfo.getId());
            if(!iDepartmentInfoService.updateById(departmentInfo)){
                return R.failed(MyCode.修改部门信息失败);
            }
            return iUserInfoService.updateUserDept(departmentInfo.getId(),departmentInfo.getDepName(),byId.getDepName());
        }
        /**
        * 删除
        */
        @DeleteMapping("/{id}")
        @ApiOperation(value = "删除", notes = "传入id")
        @RequiresPermissions("basic:department:delete")
        public R del(@ApiParam(value = "主键集合", required = true) @PathVariable("id") String id) {
            if(id==null){
                this.success("成功");
            }
            DeptUser deptUser=new DeptUser();
            deptUser.setDepId(id);
            if(iDeptUserService.getDeptUserCount(deptUser)<=0){
                return R.ok(iDepartmentInfoService.removeById(id));
            }
            return R.failed(MyCode.该部门已经分配了用户_不允许删除_请先取消该部门对应的用户);
        }

    /**
     * 列表,用于前端下拉框操作
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "列表", notes = "传入DepartmentInfo对象")
    public R<List<DepartmentInfo>> list() {
        DepartmentInfo departmentInfo=new DepartmentInfo();
        departmentInfo.setState((Character)SystemEnum.STATE_ONE.getCode());
        QueryWrapper<DepartmentInfo> queryWrapper=new QueryWrapper<>(departmentInfo);
        List<DepartmentInfo> list = iDepartmentInfoService.list(queryWrapper);
        return R.ok(list);
    }
 }
